package main

import (
	"finance/config"
	_ "finance/docs"
	"finance/internal/controllers"
	"finance/internal/middleware"
	"finance/internal/models"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"strconv"
)

func main() {
	config.InitConfig()
	models.InitDB()

	r := gin.Default()

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 解决跨域问题, 加入流量分析中间件
	r.Use(middleware.CORSMiddleware(), middleware.LogMiddleware())

	r.POST("/user/create", controllers.CreateUser) // [TODO]目前先这样
	r.POST("/api/login", controllers.Login)

	// 以下所有的接口都需要经过这两个中间件
	r.Use(middleware.JWTAuthMiddleware())
	// 获取菜单需要jwt中间件,因为需要知道role_id
	r.GET("/api/getMenus", controllers.GetMyMenus)

	// 下面的都是普通的接口,都需要经过权限检验中间件
	r.Use(middleware.PermissionMiddleware())
	account := r.Group("/account")
	{
		account.GET("/getInfo", controllers.GetMyAccount)
		account.GET("/getAccounts", controllers.GetAllAccounts)
		account.POST("/create", controllers.CreateAccount)
		account.PUT("/disable/:id", controllers.DisableAccount)
	}

	transaction := r.Group("/transaction")
	{
		transaction.POST("/deposit", controllers.DepositHandler)
		transaction.POST("/withdraw", controllers.WithdrawHandler)
		transaction.GET("/transfer")
		transaction.GET("/getRecords", controllers.GetAllTransactionsHandler)
	}

	audit := r.Group("/audit")
	{
		audit.GET("/system", controllers.GetSystemLogs)
		audit.GET("/result", controllers.GetAuditLogs)
	}

	r.Run(":" + strconv.Itoa(config.AppConfig.Server.Port))
}
